/*
 *  Copyright (c) GBA-NCTI-ISDC. 2022-2024.
 *
 *  openGauss DataKit is licensed under Mulan PSL v2.
 *  You can use this software according to the terms and conditions of the Mulan PSL v2.
 *  You may obtain a copy of Mulan PSL v2 at:
 *
 *  http://license.coscl.org.cn/MulanPSL2
 *
 *  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 *  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 *  MERCHANTABILITY OR FITFOR A PARTICULAR PURPOSE.
 *  See the Mulan PSL v2 for more details.
 *  -------------------------------------------------------------------------
 *
 *  ElasticsearchProvider.java
 *
 *  IDENTIFICATION
 *  plugins/observability-log-search/src/main/java/com/nctigba/observability/log/config/ElasticsearchProvider.java
 *
 *  -------------------------------------------------------------------------
 */

package com.nctigba.observability.log.listener;

import com.gitee.starblues.spring.MainApplicationContext;
import com.gitee.starblues.spring.SpringBeanFactory;
import org.opengauss.admin.common.core.vo.MenuVo;
import org.opengauss.admin.system.plugin.facade.MenuFacade;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @description: Plugin listener
 * @author: YangZiHao
 * @date: 2022/12/4 15:05
 */
@Component
public class PluginListener implements ApplicationListener<ApplicationEvent> {
	public static final String pluginId = "observability-log-search";

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ApplicationEnvironmentPreparedEvent) {
		} else if (event instanceof ApplicationPreparedEvent) {
		} else if (event instanceof ContextRefreshedEvent) {
		} else if (event instanceof ApplicationReadyEvent) {
			MainApplicationContext context = ((ApplicationReadyEvent) event).getApplicationContext()
					.getBean(MainApplicationContext.class);
			SpringBeanFactory factory = context.getSpringBeanFactory();
			MenuFacade menuFacade = factory.getBean(MenuFacade.class);
			if (menuFacade != null) {
				MenuVo firstMenu = menuFacade.savePluginMenu(pluginId, "智能运维", "Intelligent OPS", 4, "monitor");
				menuFacade.savePluginMenu(pluginId, "日志检索", "Log Retrieval", 12, "vem/log", firstMenu.getMenuId());
			}
		} else if (event instanceof ContextClosedEvent) {
			MainApplicationContext context = ((ContextClosedEvent) event).getApplicationContext()
					.getBean(MainApplicationContext.class);
			SpringBeanFactory factory = context.getSpringBeanFactory();
			MenuFacade menuFacade = factory.getBean(MenuFacade.class);
			if (menuFacade != null) {
				menuFacade.deletePluginMenu(pluginId);
			}
		}
	}
}
